home *** CD-ROM | disk | FTP | other *** search
- r: keymenu.rap - Put up array-based menu, respond to single keystrokes
-
- ; This is a fairly complex example showing some use of
- ; arrays, ansi codes, as, special keys, and xi.
-
- if $screentype <> "ansi"
- t:Sorry... you'll need the ANSI.SYS driver installed for this program.
- bye
- end if
-
- $bold=*chr(27)[1m ; highlight text
- $norm=*chr(27)[0m ; normal text
- $save=*chr(27)[s ; save cursor position
- $restore=*chr(27)[u ; restore cursor position
- $beep=*chr(7) ; sound bell
- $hide=*chr(27)[26;1H ; hide cursor off-screen
-
- $month[1]=January ; The options are in an arbitrary-length array.
- $month[2]=February ; Its name is also arbitrary.
- $month[3]=March ; When we call *achoose, we'll provide name and length.
- $month[4]=April
- $month[5]=May
- $month[6]=June
- $month[7]=July
- $month[8]=August
- $month[9]=September
- $month[10]=October
- $month[11]=November
- $month[12]=December
-
- proc main ; example of a procedure that uses achoose
- loop
- #choice=*achoose("month",12,7) ;array name, length, default
- exit if #choice = 0
-
- foot:You picked $month[#choice].
- end loop
-
- ca 20,1
- t:So long!
-
- end proc
-
-
- ; *achoose - Provide menu based on text array, respond to single keystrokes
- ; Highlight current selection (starting with default)
- ; F1 - F10 give options 1-10, Home gives 1, End gives last,
- ; Up/Left arrows decrement choice, Down/Right arrows increment
- ; Return "takes" the current selection. Escape bails out.
-
- num function *achoose($name,#nmax,#default)
-
- declare #n,#old,#c,#paged
-
- cls
-
- loop for #n = 1 to #nmax ; Display all the options
- th:*gospot(#n) *getm($name,#n)
- end loop
-
- #n = #default ; Highlight the default
- th:*gospot(#n)$save$bold *getm($name,#n) $norm
-
- loop
- #old = #n
-
- as:#c ; Get a keystroke (as an ascii number)
- if #c == 0 ; Special keys produce null and second char
- as:#c ; so get the second char
-
- if #c == 72 or #c == 75 ; Up or left arrow
- #n--
-
- else if #c == 80 or #c == 77 ; Down or right arrow
- #n++
-
- else if #c == 71 ; Home
- #n = 1
-
- else if #c == 79 ; End
- #n = #nmax
-
- else if #c >= 59 and #c <= 68 ; Function keys
- #n = #c-58
-
- else ; Unknown
- th:$beep ; beep
-
- end if
-
- else ; non-zero key - a normal keystroke
-
- if #c==10 or #c==13 ; Return or linefeed
- return #n
-
- else if #c==27 ; Escape - bail out
- return 0
-
- else
- th:$beep ; Invalid - beep
- repeat
-
- end if
- end if
-
- if #n < 1 ; keep n in limits
- #n = #nmax
-
- else if #n > #nmax
- #n = 1
-
- end if
- ; change the highlight
- th:$restore *getm($name,#old) *gospot(#n)$save$bold *getm($name,#n) $norm$hide
-
- end loop
-
- end function
-
- string function *gospot(#n) ; go to the spot for a given selection
- declare #row
- #row = #n+2
- return "*chr(27)[#row;10\H"
- end function
-
- string function *getm($name,#sub) ; get member given plain array name,subscript
- xi:return "\$$name\[#sub]"
- end function
-